package com.smartleanx.module.hrm.controller.admin.operationlog;

import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import com.smartleanx.framework.excel.core.util.ExcelUtils;
import com.smartleanx.framework.operatelog.core.annotations.OperateLog;
import com.smartleanx.module.hrm.controller.admin.operationlog.vo.OperationLogPageReqVO;
import com.smartleanx.module.hrm.controller.admin.operationlog.vo.OperationLogRespVO;
import com.smartleanx.module.hrm.controller.admin.operationlog.vo.OperationLogSaveReqVO;
import com.smartleanx.module.hrm.dal.dataobject.operationlog.OperationLogDO;
import com.smartleanx.module.hrm.service.operationlog.OperationLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.smartleanx.framework.common.pojo.CommonResult.success;
import static com.smartleanx.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 操作日志记录")
@RestController
@RequestMapping("/hrm/operation-log")
@Validated
public class OperationLogController {

    @Resource
    private OperationLogService operationLogService;

    @PostMapping("/create")
    @Operation(summary = "创建操作日志记录")
    @PreAuthorize("@ss.hasPermission('hrm:operation-log:create')")
    public CommonResult<Long> createOperationLog(@Valid @RequestBody OperationLogSaveReqVO createReqVO) {
        return success(operationLogService.createOperationLog(createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得操作日志记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hrm:operation-log:query')")
    public CommonResult<OperationLogRespVO> getOperationLog(@RequestParam("id") Long id) {
        OperationLogDO operationLog = operationLogService.getOperationLog(id);
        return success(BeanUtils.toBean(operationLog, OperationLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得操作日志记录分页")
    @PreAuthorize("@ss.hasAnyPermissions('hrm:operation-log:query', 'hrm:payroll:log')")
    public CommonResult<PageResult<OperationLogRespVO>> getOperationLogPage(@Valid OperationLogPageReqVO pageReqVO) {
        PageResult<OperationLogRespVO> pageResult = operationLogService.getOperateLogPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出操作日志记录 Excel")
    @PreAuthorize("@ss.hasPermission('hrm:operation-log:export')")
    @OperateLog(type = EXPORT)
    public void exportOperationLogExcel(@Valid OperationLogPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OperationLogRespVO> list = operationLogService.getOperateLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "操作日志记录.xlsx", "数据", OperationLogRespVO.class, list);
    }

}
